//
//  AppDelegate+Updater.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/4/4.
//  Copyright © 2019 YueWen. All rights reserved.
//

import Foundation


extension QDMetroAppDelegate {
    
    //检测版本，
    func checkVersionForApp(){
        
        //进行版本检测
        QDNetworkingManager.request(url: updateUrl, paramteres: [String: String](), success: { (msg, info) in
            
            guard let isUpdate = info["isUpdate"] as? Bool, isUpdate == true else { return }//如果是更新
            // 获得更新内容
            let content = ((info["content"] as? String ?? "") as NSString).replacingOccurrences(of: "\\r\\n", with: "\\n")

            // 数据源
            let titles = (content as NSString).components(separatedBy: "\\n")
            // 展示，并获取数据
            print("\(content)")
            QDVersionView.show(titles: titles)
        })
    }
    
    /// 请求重要信息以及版本检测
    @objc func checkImportantMessageOrVersion(){
        
        //进行重要信息
        QDNetworkingManager.request(url: importanceMessageUrl, success: { (msg, data) in
            guard let isExist = data["exist"] as? Bool, isExist == true else {
                //请求版本检测
                self.checkVersionForApp()
                return;
            }
            
            // 获得显示的内容
            let (title,topTitle,detail) = (data["title"] as? String,data["name"] as? String,data["content"] as? String)
            
            QDImportantMessageView.show(title: title, topTitle: topTitle, detail: detail)
            
        }) { (_, _) in
            
            // 进行版本检测
            self.checkVersionForApp()
        }
    }
}
